﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Rapor;

using DevTeam;

using System;
namespace ConfExpert.Forms.Ex
{
    public partial class _CC_Barkod : XForm
    {
        public _CC_Barkod()
        {
            InitializeComponent();
        }

        private void CC_Barkod_Load(object sender, EventArgs e)
        {
            
            btnKolile.Click += btnKolile_Click;
            gridView1.Init("GetCC_Barkod");
            gridView1.AddSummary("Adet");
            gridView1.BestFitColumns();

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Rapor", Ac);
            gridView1.MoveLast();

            this.Shown += CC_Barkod_Shown;
        }

        void CC_Barkod_Shown(object sender, EventArgs e)
        {
            memoEdit1.Focus();
        }

        private void Ac(object sender, EventArgs e)
        {
            string BoxNoS = gridView1.GetSelectedValues("BoxNo");
            string[] B = BoxNoS.Split(",".ToCharArray());
            CC_Sayim cc = new CC_Sayim(int.Parse(B[0]));
            cc.CreateDocument();

            for (int i = 1; i < B.Length; i++)
            {
                CC_Sayim cci = new CC_Sayim(int.Parse(B[i]));
                cci.CreateDocument();
                cc.Pages.AddRange(cci.Pages);
            }
            rp.View(cc, false);
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {
                int BoxNo = gridView1.GetFocusedDataRow()["BoxNo"].ToInt();

                CC_Sayim cc = new CC_Sayim(BoxNo);
                rp.View(cc);
            }
        }

        void btnKolile_Click(object sender, EventArgs e)
        {
            string[] lst = memoEdit1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            string barcodes = String.Join(",", lst);

            if (barcodes.Length > 1)
            {
                gridView1.RefreshDataSource(false, "InsCC_Barkod", barcodes);
                gridView1.MoveLast();
                //gridView1.FocusedRowHandle = gridView1.LocateByDisplayText(0, gridView1.Columns["BoxNo"], BoxNo);
                gridView1.BestFitColumns();
                memoEdit1.Text = string.Empty;
                memoEdit1.Focus();
            }
        }
    }
}
